<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>Creating a simple XML document</title>
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-base.css" />
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-medium.css" />

 </head>
 <body class="docs"><div class="navbar navbar-fixed-top">
  <div class="navbar-inner clearfix">
    <ul class="nav" style="width: 100%">
      <li style="float: left;"><a href="xmlwriter.examples.html">« 范例</a></li>
      <li style="float: right;"><a href="example.xmlwriter-namespace.html">Working with XML namespaces »</a></li>
    </ul>
  </div>
</div>
<div id="breadcrumbs" class="clearfix">
  <ul class="breadcrumbs-container">
    <li><a href="index.html">PHP Manual</a></li>
    <li><a href="xmlwriter.examples.html">范例</a></li>
    <li>Creating a simple XML document</li>
  </ul>
</div>
<div id="layout">
  <div id="layout-content"><div id="example.xmlwriter-simple" class="section">
  <h2 class="title">Creating a simple XML document</h2>
  <p class="para">
   This example shows how to use XMLWriter to create a XML document in memory.
  </p>
  <p class="para">
   <div class="example" id="example-5489">
    <p><strong>示例 #1 Creating a simple XML document</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /><br />$xw&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">xmlwriter_open_memory</span><span style="color: #007700">();<br /></span><span style="color: #0000BB">xmlwriter_set_indent</span><span style="color: #007700">(</span><span style="color: #0000BB">$xw</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">1</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$res&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">xmlwriter_set_indent_string</span><span style="color: #007700">(</span><span style="color: #0000BB">$xw</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'&nbsp;'</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">xmlwriter_start_document</span><span style="color: #007700">(</span><span style="color: #0000BB">$xw</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'1.0'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'UTF-8'</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">//&nbsp;A&nbsp;first&nbsp;element<br /></span><span style="color: #0000BB">xmlwriter_start_element</span><span style="color: #007700">(</span><span style="color: #0000BB">$xw</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'tag1'</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">//&nbsp;Attribute&nbsp;'att1'&nbsp;for&nbsp;element&nbsp;'tag1'<br /></span><span style="color: #0000BB">xmlwriter_start_attribute</span><span style="color: #007700">(</span><span style="color: #0000BB">$xw</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'att1'</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">xmlwriter_text</span><span style="color: #007700">(</span><span style="color: #0000BB">$xw</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'valueofatt1'</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">xmlwriter_end_attribute</span><span style="color: #007700">(</span><span style="color: #0000BB">$xw</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">xmlwriter_write_comment</span><span style="color: #007700">(</span><span style="color: #0000BB">$xw</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'this&nbsp;is&nbsp;a&nbsp;comment.'</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">//&nbsp;Start&nbsp;a&nbsp;child&nbsp;element<br /></span><span style="color: #0000BB">xmlwriter_start_element</span><span style="color: #007700">(</span><span style="color: #0000BB">$xw</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'tag11'</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">xmlwriter_text</span><span style="color: #007700">(</span><span style="color: #0000BB">$xw</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'This&nbsp;is&nbsp;a&nbsp;sample&nbsp;text,&nbsp;ä'</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">xmlwriter_end_element</span><span style="color: #007700">(</span><span style="color: #0000BB">$xw</span><span style="color: #007700">);&nbsp;</span><span style="color: #FF8000">//&nbsp;tag11<br /><br /></span><span style="color: #0000BB">xmlwriter_end_element</span><span style="color: #007700">(</span><span style="color: #0000BB">$xw</span><span style="color: #007700">);&nbsp;</span><span style="color: #FF8000">//&nbsp;tag1<br /><br /><br />//&nbsp;CDATA<br /></span><span style="color: #0000BB">xmlwriter_start_element</span><span style="color: #007700">(</span><span style="color: #0000BB">$xw</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'testc'</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">xmlwriter_write_cdata</span><span style="color: #007700">(</span><span style="color: #0000BB">$xw</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"This&nbsp;is&nbsp;cdata&nbsp;content"</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">xmlwriter_end_element</span><span style="color: #007700">(</span><span style="color: #0000BB">$xw</span><span style="color: #007700">);&nbsp;</span><span style="color: #FF8000">//&nbsp;testc<br /><br /></span><span style="color: #0000BB">xmlwriter_start_element</span><span style="color: #007700">(</span><span style="color: #0000BB">$xw</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'testc'</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">xmlwriter_start_cdata</span><span style="color: #007700">(</span><span style="color: #0000BB">$xw</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">xmlwriter_text</span><span style="color: #007700">(</span><span style="color: #0000BB">$xw</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"test&nbsp;cdata2"</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">xmlwriter_end_cdata</span><span style="color: #007700">(</span><span style="color: #0000BB">$xw</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">xmlwriter_end_element</span><span style="color: #007700">(</span><span style="color: #0000BB">$xw</span><span style="color: #007700">);&nbsp;</span><span style="color: #FF8000">//&nbsp;testc<br /><br />//&nbsp;A&nbsp;processing&nbsp;instruction<br /></span><span style="color: #0000BB">xmlwriter_start_pi</span><span style="color: #007700">(</span><span style="color: #0000BB">$xw</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'php'</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">xmlwriter_text</span><span style="color: #007700">(</span><span style="color: #0000BB">$xw</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'$foo=2;echo&nbsp;$foo;'</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">xmlwriter_end_pi</span><span style="color: #007700">(</span><span style="color: #0000BB">$xw</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">xmlwriter_end_document</span><span style="color: #007700">(</span><span style="color: #0000BB">$xw</span><span style="color: #007700">);<br /><br />echo&nbsp;</span><span style="color: #0000BB">xmlwriter_output_memory</span><span style="color: #007700">(</span><span style="color: #0000BB">$xw</span><span style="color: #007700">);</span>
</span>
</code></div>
    </div>

    <div class="example-contents"><p>以上例程会输出：</p></div>
    <div class="example-contents screen">
<div class="cdata"><pre>
&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
&lt;tag1 att1=&quot;valueofatt1&quot;&gt;
 &lt;!--this is a comment.--&gt;
 &lt;tag11&gt;This is a sample text, ä&lt;/tag11&gt;
&lt;/tag1&gt;
&lt;testc&gt;&lt;![CDATA[This is cdata content]]&gt;&lt;/testc&gt;
&lt;testc&gt;&lt;![CDATA[test cdata2]]&gt;&lt;/testc&gt;
&lt;?php $foo=2;echo $foo;?&gt;
</pre></div>
    </div>
   </div>
  </p>
 </div></div></div></body></html>